/*
 * @(#)CollectionUtil.java
 *
 * @Company <Jinher Technology Development Company LTD.>
 */

package com.cn.system.common.util;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Set;

public class CollectionUtil {

	//将Set对象转为List对象
	@SuppressWarnings("unchecked")
	public static List setToList(Set set) {
		List list = new ArrayList();

		if (set == null) {
			return list;
		}

		for (Iterator it = set.iterator(); it.hasNext();) {
			list.add(it.next());
		}

		return list;
	}

	/**
	 * @param list
	 * @return
	 * @author liuls
	 * 去除list里的重复值
	 */
	@SuppressWarnings("unchecked")
	public static List removeDuplicateObj(List list) {

		Set someSet = new HashSet(list);
		Iterator iterator = someSet.iterator();
		List tempList = new ArrayList();
		while (iterator.hasNext()) {
			tempList.add(iterator.next().toString());
		}
		return tempList;

	}

	public static <T> String joinList(List<T> list, String separator){
		StringBuffer stringBuffer = new StringBuffer();
		if(list != null && list.size() > 0){
			for(int i=0; i<list.size(); i++){
				T obj = list.get(i);
				if(i < list.size() - 1){
					stringBuffer.append(obj.toString()).append(separator);
				}
				else{
					stringBuffer.append(obj.toString());
				}
				
			}
			return stringBuffer.toString();
		}
		else {
			return "";
		}
	}
	
	public static <T> String joinList(List<T> list, String separator, String wrap){
		StringBuffer stringBuffer = new StringBuffer();
		if(list != null && list.size() > 0){
			for(int i=0; i<list.size(); i++){
				T obj = list.get(i);
				stringBuffer.append(wrap).append(obj.toString()).append(wrap);
				if(i < list.size() - 1){
					stringBuffer.append(separator);
				}
				
			}
			return stringBuffer.toString();
		}
		else {
			return "";
		}
	}
	
	public static <T> List<T> convertArrayToList(T[] array){
		List<T> list = new ArrayList<T>();
		for(T obj: array){
			list.add(obj);
		}
		return list;
	}
}